package icangyu.base

import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.appcompat.app.AppCompatActivity
import android.text.TextUtils
import icangyu.base.views.ProgressDialog
import java.util.*

abstract class BaseActivity:AppCompatActivity(){

    private var progressDialog:ProgressDialog?=null

    fun isProgressShowing(): Boolean {
        return progressDialog != null && progressDialog!!.isShowing()
    }

    fun showProgress() {
        showProgress(null)
    }

    fun showProgress(msg: String?) {
        var msg = msg
        if (progressDialog == null) {
            if (TextUtils.isEmpty(msg)) {
                msg = "视频处理中"
            }
            progressDialog = ProgressDialog(this, msg)
        }
        progressDialog?.show()
    }

    fun hideProgress() {
        try {
            if (progressDialog != null && progressDialog!!.isShowing()) {
                progressDialog!!.dismiss()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            progressDialog = null
        }

    }

    protected fun checkPermission(permission: Array<String>, requestCode: Int): Boolean {
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                val list = ArrayList<String>()
                for (p in permission) {
                    if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(applicationContext, p)) {
                        list.add(p)
                    }
                }
                if (list.size>0) {
                    val a = arrayOfNulls<String>(list.size)
                    ActivityCompat.requestPermissions(this, list.toArray(a), requestCode)
                    return false
                }
            } catch (e: Exception) {
                return false
            }

        }
        return true
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

}
